// ignore_for_file: file_names, slash_for_doc_comments, must_call_super, unnecessary_overrides, override_on_non_overriding_member, must_be_immutable, prefer_const_constructors, sized_box_for_whitespace, avoid_unnecessary_containers, avoid_print

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mybase/base/abstract/IFFR.dart';
import 'package:mybase/base/abstract/IFFRefresh.dart';
import 'package:mybase/base/abstract/IFFScaffold.dart';
import 'package:mybase/base/super/FFInk.dart';
import 'package:mybase/base/tool/color/FFColor.dart';
import 'package:mybase/base/widget/layout/FFImageButton.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFNavi.dart';
import 'package:mybase/base/widget/layout/FFStackContainer.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';
import 'package:mybase/base/widget/page/FFScaffold.dart';
import 'package:mybase/base/widgetTool/image/FFImageUtil.dart';

/**普通页面
 * 注意事项
 * 必须继承FFScaffold，FFScaffoldState，必须with MFFScaffold
 * 必须重写 iffScaffold，widget
 * 禁止重写initState,build,
 * 所有的父类属性，方法几乎是ff开头
 */
class FFSliverAppBarPage extends FFScaffold {
  String? name;

  FFSliverAppBarPage({Key? key, this.name}) : super(key: key);

  @override
  FFSliverAppBarPageState createState() => FFSliverAppBarPageState();
}

class FFSliverAppBarPageState extends FFScaffoldState with MFFScaffold {
  /**固定写法 */
  @override
  IFFScaffold get iffScaffold => this;

  @override
  FFSliverAppBarPage get widget => super.widget as FFSliverAppBarPage;

  /**固定写法end */
  /**刷新控件，不要刷新功能，则此值不传 */
  @override
  IFFRefresh? get iffRefresh => FFSliverAppBarPageStateRefresh();

  ///刷新控制

  @override
  void ffInitState() {
    super.ffInitState();
    iffScaffold.ffPageStatus = FFPageStatus.result;
  }

  // sliverappbar 效果，此方法不需要实现，或返回null
  @override
  Widget? ffNavi(BuildContext context) {
    return null;
  }

  @override
  Widget? ffFirstLoadingWidget(BuildContext context) {
    return FFTextButton(
      color: Colors.yellow,
      text: "这是第一次菊花提示页",
    );
  }
  //kToolbarHeight 默认导航栏高度 ，定义在Flutter/packages/flutter/lib/src/material/constants.dart

  @override
  Widget? ffBody(BuildContext context) {
    List<Widget> slivers = [
      Theme(
        //单独给子组件应用主题色
        data: ThemeData(
          primarySwatch: FFColor.createMaterialColor(Colors.black),
        ),
        child: SliverAppBar(
          title: Text("标题"),
          expandedHeight: kToolbarHeight + 44.0,
          floating: false,
          pinned: true,
          snap: false,
          leading: IconButton(
            icon: Icon(Icons.arrow_back_ios_new_outlined),
            onPressed: () {
              if (Navigator.canPop(context)) Navigator.pop(context);
            },
          ),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.add),
              onPressed: () {
                debugPrint("添加");
              },
            ),
            IconButton(
              icon: Icon(Icons.more_horiz),
              onPressed: () {
                debugPrint("更多");
              },
            ),
          ],
          flexibleSpace: FlexibleSpaceBar(
            background: FFImageFactory.netWork(
                "https://t7.baidu.com/it/u=4240641596,3235181048&fm=193&f=GIF"),
          ),
        ),
      ),
      SliverFixedExtentList(
        itemExtent: 50.0,
        delegate: SliverChildBuilderDelegate(
          (context, index) => ListTile(
            title: Text("Item $index"),
          ),
          childCount: 30,
        ),
      )
    ];


    //ClampingScrollPhysics 边缘位置不让滑
    return CustomScrollView(
      slivers: slivers,
      physics: ClampingScrollPhysics(),
    );
  }
}

class FFSliverAppBarPageStateRefresh with MFFRefresh {
  @override
  bool? ffCanRefreshHead() {
    return true;
  }

  @override
  bool? ffCanRefreshFoot() {
    return false;
  }

  @override
  R ffRefresh(bool isHead) {
    debugPrint("调用接口...");
    Future.delayed(Duration(seconds: 2), () {
      ffRefreshCompleted();
    });
    return R();
  }
}
